package com.github.gaojh.server.handler;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.github.gaojh.server.exception.FlyingException;
import com.github.gaojh.server.http.HttpContext;
import com.github.gaojh.server.multipart.MimeType;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.*;

import java.nio.charset.StandardCharsets;
import java.util.Map;

import static io.netty.handler.codec.http.HttpResponseStatus.FOUND;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;


/**
 * 构建返回对象的数据工具集合
 *
 * @author gaojh
 */
public class BuildResponse {

    /**
     * @param httpContext
     * @return
     */
    public static FullHttpResponse buildStaticResourceResponse(HttpContext httpContext) {
        FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(httpContext.getStaticResource().getBytes()));
        resp.headers().set(HttpHeaderNames.CONTENT_TYPE, MimeType.get(httpContext.getStaticResource().getSuffix()) + ";charset=UTF-8");
        return resp;
    }

    /**
     * @param httpContext
     * @return
     */
    public static FullHttpResponse buildDownloadResponse(HttpContext httpContext) {
        FullHttpResponse response;
        if (httpContext.getHttpResponse().getFile() == null) {
            response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(IoUtil.readBytes(httpContext.getHttpResponse().getInputStream())));
        } else {
            response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(FileUtil.readBytes(httpContext.getHttpResponse().getFile())));
        }
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/octet-stream;charset=UTF-8");
        response.headers().add(HttpHeaderNames.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", httpContext.getHttpResponse().getFileName()));
        return response;
    }


    /**
     * @param httpContext
     * @return
     */
    public static FullHttpResponse buildJsonResponse(HttpContext httpContext) {
        return new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(httpContext.getHttpResponse().getJson().getBytes(StandardCharsets.UTF_8)));
    }

    /**
     * @param httpContext
     * @return
     */
    public static FullHttpResponse buildHtmlResponse(HttpContext httpContext) {
        return new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(httpContext.getHttpResponse().getHtml().getBytes(StandardCharsets.UTF_8)));
    }


    /**
     * @param httpContext
     * @return
     */
    public static FullHttpResponse buildRouteInvokeResponse(HttpContext httpContext) {
        //是否是方法调用的
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(httpContext.getResult().getBytes(StandardCharsets.UTF_8)));
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain;charset=UTF-8");
        return response;
    }


    /**
     * @param httpContext
     * @return
     */
    public static FullHttpResponse postSetHeader(FullHttpResponse response, HttpContext httpContext) {
        response.headers().set(HttpHeaderNames.SERVER, "FlyingServer");
        response.headers().set("FlyingServer", "4.0.0");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        Map<String, String> headers = httpContext.getHttpResponse().getHeaders();
        headers.forEach((a, b) -> {
            response.headers().set(a, b);
            if ("location".equals(a)) {
                response.setStatus(FOUND);
            }
        });
        return response;
    }


    /**
     * @param e
     * @return
     */
    public static FullHttpResponse buildErrorResponse(Throwable e) {
        FlyingException cause = (FlyingException) e.getCause();
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(cause.getHttpCode()), Unpooled.wrappedBuffer(cause.getResponseMessage().getBytes(StandardCharsets.UTF_8)));
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain;charset=UTF-8");
        response.headers().set("FlyingServer", "4.0.0");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

        return response;
    }


}
